﻿using System;
using System.Linq;
using Biz.EF.Gateway.Monitor;
using Biz.EF.Platform.Kq;

using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    public class ProcessorDownline
    {
        private static ILog log = LogManager.GetLogger(typeof(ProcessorDownline));
        public static void GenDownlineCmdToGateway(GatewayMonitorContext gCtx, PlatformYgContext pCtx)
        {
            try
            {
                var cmds = pCtx.T_transmittal.Where(t => t.Type_ID.Equals("1"))
                     .Where(t => t.Flag_process.Equals("0"))
                     .Where(t => !(t.CN.Equals("2081"))).Take(500).ToList();
                foreach (var downcmd in cmds)
                {
                    downcmd.Flag_process = "1";
                    var resultStr = "QN=" + downcmd.QN + ";ST=32;" + "CN=" + downcmd.CN + ";PW=123456" + ";MN=" + downcmd.MN
                                        + ";Flag=0;CP=&&" + downcmd.CP + "&&";
                    resultStr = Crc.WrapByCrc(resultStr,"total");
                    var now = DateTime.Now;
                    send_ctr_cmd cmd = new send_ctr_cmd
                    {
                        cn = downcmd.CN,
                        mn = downcmd.MN,
                        qn = downcmd.QN,
                        is_sent = false,
                        send_string = resultStr,
                        sent_time = now,
                        send_time = now
                    };
                    gCtx.send_ctr_cmd.Add(cmd);

                    var result = pCtx.T_transmittal_Exec_Result
                        .Where(t => t.QN == downcmd.QN)
                        .Where(t => t.ST == downcmd.ST)
                        .Where(t => t.MN == downcmd.MN).FirstOrDefault(t => t.CN == downcmd.CN);
                    if (result == null)
                    {
                        result = new T_transmittal_Exec_Result
                        {
                            QN = downcmd.QN,
                            ST = downcmd.ST,
                            CN = downcmd.CN,
                            MN = downcmd.MN,
                            Flag = downcmd.Flag,
                            SendValue = null,
                            ReturnValue = null,
                            Flag_process = "0",
                            Reason = null,
                            OperateDate = now
                        };
                        pCtx.T_transmittal_Exec_Result.Add(result);
                    }
                }
                pCtx.SaveChanges();
                gCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }

        }
        public static void GenDownlineCmd3093ToGateway(GatewayMonitorContext gCtx, PlatformYgContext pCtx)
        {
            try
            {
                var cmds = pCtx.Envi_Transmittal_Plan.Where(t => t.CN.Equals("3093"))
                    .Where(t => !t.Flag_process.Equals("1"))
                    .Where(t => t.ExecSuccess == true).OrderBy(t => t.StartTime).Take(1000).ToList();
                foreach (var downcmd in cmds)
                {
                    downcmd.Flag_process = "1";
                    var resultStr = "QN=" + downcmd.QN + ";ST=32;" + "CN=" + downcmd.CN + ";PW=123456" + ";MN=" + downcmd.MN
                                        + ";Flag=0;CP=&&" + downcmd.CP + "&&";
                    resultStr = Crc.WrapByCrc(resultStr,"total");
                    var now = DateTime.Now;
                    send_ctr_cmd cmd = new send_ctr_cmd
                    {
                        cn = downcmd.CN,
                        mn = downcmd.MN,
                        qn = downcmd.QN,
                        is_sent = false,
                        send_string = resultStr,
                        sent_time = now,
                        send_time = now
                    };
                    gCtx.send_ctr_cmd.Add(cmd);
                }
                pCtx.SaveChanges();
                gCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }

        }
        public static void GenDownlineDataToGateway(GatewayMonitorContext gCtx, PlatformYgContext pCtx)
        {
            try
            {
                var cmds = pCtx.T_transmittal.Where(t => t.Type_ID.Equals("1"))
                     .Where(t => t.Flag_process.Equals("0"))
                     .Where(t => t.CN.Equals("2081")).Take(500).ToList();
                foreach (var downcmd in cmds)
                {
                    downcmd.Flag_process = "1";
                    var resultStr = "QN=" + downcmd.QN + ";ST=32;" + "CN=" + downcmd.CN + ";PW=123456" + ";MN=" + downcmd.MN
                                        + ";Flag=0;CP=&&" + downcmd.CP + "&&";
                    resultStr = Crc.WrapByCrc(resultStr,"total");
                    var now = DateTime.Now;
                    send_ctr_cmd cmd = new send_ctr_cmd
                    {
                        cn = downcmd.CN,
                        mn = downcmd.MN,
                        qn = downcmd.QN,
                        is_sent = false,
                        send_string = resultStr,
                        sent_time = now,
                        send_time = now
                    };
                    gCtx.send_ctr_cmd.Add(cmd);
                }
                pCtx.SaveChanges();
                gCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }

        }
    }
}
